
//项目路径
var strPath = window.document.location.pathname;
//var postPath = strPath.substring(0, strPath.substr(1).indexOf('/') + 1);
var postPath = "/gdp";
var serviceId = GetQueryString("serviceId");
var serviceType = GetQueryString("serviceType");
var masterDetailId = GetQueryString("masterDetailId");
var operateMethod = GetQueryString("operateMethod");
console.log("luowen operateMethod", operateMethod, isAddMode())

// here Test transfer data into
var jsonDataList = JSON.parse(decode_utf8(GetQueryString("jsonDataList")));
console.log("luowen dataList", jsonDataList)
$(function(){
    //加载数据
    if(isAddMode()){
        $("#tb_fieldList").bootstrapTable({
            url : postPath + '/system/showfield/getNotExistsShowFieldInService',
            undefinedText : '-',
            striped : true, // 是否显示行间隔色
            queryParams : {serviceId:serviceId,serviceType:serviceType,masterDetailId:masterDetailId},
            uniqueId : "key", // 每一行的唯一标识
            columns:[{
                field : 'state',
                checkbox : true,
                align : 'center',
                valign : 'middle'
            },{
                title : '字段名称',
                field : 'fieldname',
                align : 'center',
                valign : 'middle'
            },{
                title: '自定义字段名称',
                field: 'customAlias',
                align: 'center',
                valign: 'middle',
                editable: {
                    type: 'text',
                    title: '自定义字段名称',
                    validate: function (v) {
                    }
                }
            },{
                title: '字段标题名称',
                field: 'fieldnameCn',
                align: 'center',
                valign: 'middle'
            }, {
                title: '自定义字段标题名称',
                field: 'customFieldNameAlias',
                align: 'center',
                valign: 'middle',
                editable: {
                    type: 'text',
                    title: '中文名称',
                    validate: function (v) {
                    }
                }
            }, {
                title: '字段显示顺序',
                field: 'showorder',
                align: 'center',
                width: '100px',
                valign: 'middle',
                editable: {
                    type: 'text',
                    title: '字段显示顺序',
                    validate: function (v) {
                    }
                }
            }]
        });

    }else{
        $("#tb_fieldList").bootstrapTable({
            data : jsonDataList,
            undefinedText : '-',
            striped : true, // 是否显示行间隔色
            queryParams : jsonDataList,
            uniqueId : "key", // 每一行的唯一标识
            columns:[{
                title : 'GUID',
                field : 'guid',
                align : 'center',
                valign : 'middle',
                visible: false
            },{
                title : '字段名称',
                field : 'fieldname',
                align : 'center',
                valign : 'middle'
            },{
                title: '自定义字段名称',
                field: 'customAlias',
                align: 'center',
                valign: 'middle',
                editable: {
                    type: 'text',
                    title: '自定义字段名称',
                    validate: function (v) {
                    }
                }
            },{
                title: '字段标题名称',
                field: 'fieldnameCn',
                align: 'center',
                valign: 'middle'
            }, {
                title: '自定义字段标题名称',
                field: 'customFieldNameAlias',
                align: 'center',
                valign: 'middle',
                editable: {
                    type: 'text',
                    title: '中文名称',
                    validate: function (v) {
                    }
                }
            }, {
                title: '字段显示顺序',
                field: 'showorder',
                align: 'center',
                width: '100px',
                valign: 'middle',
                editable: {
                    type: 'text',
                    title: '字段显示顺序',
                    validate: function (v) {
                    }
                }
            }]
        });
    }

    //新增
    $("#btn_fieldAdd").click(function(){
        var row = $('#tb_fieldList').bootstrapTable('getSelections');
        if (row == null || row == "") {
            bootbox.alert("请选择一行!");
            return;
        }
        var showfieldList = [];
        $.each(row,function(i,e){
            var data = {"serviceid":serviceId,
                        "servicetype": serviceType,
                        "fieldaddonsguid":e.fieldaddonsguid,
                        "customAlias":e.customAlias,
                        "customFieldNameAlias":e.customFieldNameAlias,
                        "showorder":e.showorder,
                        "masterdetailid":masterDetailId};

            showfieldList.push(data);
        });
        $.ajax({
            type:"POST",
            url: postPath + "/system/showfield/addShowField",
            dataType:"json",
            contentType:"application/json",
            data:JSON.stringify(showfieldList),
            success:function(data){
                // bootbox.alert("新增成功!");
                showMsg('新增成功!', 'success');
                $('#tb_fieldList').bootstrapTable('refresh');
                // parent.closeModul();
            },
            error:function(){
                // bootbox.alert("新增失败,请刷新后重试!");
                showMsg('新增失败,请刷新后重试!!', 'error');
            }
        });
    });

    // 修改
    $("#btn_fieldUpdate").click(function(){
        var row = $('#tb_fieldList').bootstrapTable('getData');

        var showfieldList = [];
        $.each(row,function(i,e){
            var data = {"serviceid":serviceId,
                        "guid":e.guid,
                        "servicetype":serviceType,
                        "fieldaddonsguid":e.fieldaddonsguid,
                        "customAlias":e.customAlias,
                        "customFieldNameAlias":e.customFieldNameAlias,
                        "showorder":e.showorder,
                        "masterdetailid":masterDetailId};

            showfieldList.push(data);
        });

        $.ajax({
            type:"POST",
            url: postPath + "/system/showfield/updateShowFieldList",
            dataType:"json",
            contentType:"application/json",
            data:JSON.stringify(showfieldList),
            success:function(data){
                // bootbox.alert("新增成功!");
                showMsg('新增成功!', 'success');
                $('#tb_fieldList').bootstrapTable('refresh');
                parent.closeModul();
            },
            error:function(){
                // bootbox.alert("新增失败,请刷新后重试!");
                showMsg('新增失败,请刷新后重试!!', 'error');
            }
        });
    });



})

function GetQueryString(name){
    var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
    var r = window.location.search.substr(1).match(reg);
    if(r!=null)return  unescape(r[2]); return null;
}

function showMsg(text, icon) {
    $.toast().reset('all');
    $.toast({
        text: text,
        icon: icon,
        showHideTransition: 'fade',
        allowToastClose: true,
        hideAfter: 1500,
        stack: false,
        position: 'top-right',
        textAlign: 'left',
        loader: true,
        loaderBg: '#9ec600'
    });
}

function isAddMode(){
    var isAddMode = operateMethod == 'add';
    var addButton = document.getElementById("btn_fieldAdd")
    var updateButton = document.getElementById("btn_fieldUpdate")


    if(isAddMode){
        // add show update hidden
        addButton.style.display = "block";
        updateButton.style.display = "none";

    }else{
        // update show add hidden
        addButton.style.display = "none";
        updateButton.style.display = "block";
    }
    return  isAddMode;
}

function encode_utf8(s) {
  return encodeURIComponent(s);
}

function decode_utf8(s) {
  return decodeURIComponent(escape(s));
}
